//
//  RatesCell.m
//  PriceOnline
//
//  Created by Le Anh Tai on 8/11/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "RatesCell.h"
#import "UIConstants.h"
#import "Global.h"
#import "Utility.h"

@implementation RatesCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {                
        _codeLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(14) textColor:_textColor];
        [_codeLabel setTextAlignment:NSTextAlignmentLeft];
        [_subView addSubview:_codeLabel];
        
        _iconCountry = [[UIImageView alloc] init];
        [_subView addSubview:_iconCountry];
        
        _buyLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(14) textColor:_textColor];
        [_subView addSubview:_buyLabel];
        
        _changedBuyLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(12) textColor:_textColor];
        _changedBuyLabel.numberOfLines = 2;
        [_subView addSubview:_changedBuyLabel];
        
        _transferLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(14) textColor:_textColor];
        [_subView addSubview:_transferLabel];
        
        _changedTransferLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(12) textColor:_textColor];
        _changedTransferLabel.numberOfLines = 2;
        [_subView addSubview:_changedTransferLabel];
        
        _sellLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(14) textColor:_textColor];
        [_subView addSubview:_sellLabel];
        
        _changedSellLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(12) textColor:_textColor];
        _changedSellLabel.numberOfLines = 2;
        [_subView addSubview:_changedSellLabel];        
        
        _buyLabel.textAlignment = _changedBuyLabel.textAlignment = _changedSellLabel.textAlignment = _changedTransferLabel.textAlignment = _transferLabel.textAlignment = _sellLabel.textAlignment = UITextAlignmentRight;
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    NSInteger paddingRight = 20;
    NSInteger typeWid = (self.contentView.width - paddingRight)/5;
    NSInteger paddingTop = 4;

    _lineView.frame = RECT(0, 0, _subView.width, 2);
    
    NSInteger iconWid = 32;
    NSInteger iconHei = 24;
    
    _iconCountry.frame = RECT(typeWid - iconWid - 4, (self.height - iconHei)/2, iconWid, iconHei);
    
    _codeLabel.frame = RECT(typeWid + 4, 0, typeWid - 4, _subView.height);
    _buyLabel.frame = RECT(_codeLabel.right, 0, typeWid, _subView.height/3 + paddingTop);
    _changedBuyLabel.frame = RECT(_codeLabel.right, _buyLabel.height, typeWid, _subView.height-_buyLabel.bottom - paddingTop);
    
    _transferLabel.frame = RECT(_buyLabel.right, 0, typeWid, _subView.height/3 + paddingTop);
    _changedTransferLabel.frame = RECT(_buyLabel.right, _transferLabel.height, typeWid, _subView.height-_transferLabel.bottom - paddingTop);
    
    _sellLabel.frame = RECT(_transferLabel.right, 0,
                            self.contentView.width - _transferLabel.right - paddingRight, _subView.height/3 + paddingTop);
    _changedSellLabel.frame = RECT(_transferLabel.right, _sellLabel.height,
                                   self.contentView.width - _transferLabel.right - paddingRight, _subView.height-_sellLabel.bottom - paddingTop);
}

- (void)loadDataWith:(NSString*)type buy:(NSString*)buy ck:(NSString*)ck sell:(NSString*)sell
{
    _codeLabel.text = type;
    _buyLabel.text = buy;
    _transferLabel.text = ck;
    _sellLabel.text = sell;
}

- (void)reloadTheme
{
    [super reloadTheme];
    
    _codeLabel.textColor = _textColor;
    _buyLabel.textColor = _textColor;
    _sellLabel.textColor = _textColor;
    _transferLabel.textColor = _textColor;
}

- (void)loadDataWithDictionary:(NSDictionary*)dict
{
    NSString *code = [dict objectForKey:RATE_CODE_KEY];
    BOOL is2Lines = IS_IPHONE;
    
    // CODE -------------
    _iconCountry.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", code]];
    [self setLable:code forLabel:_codeLabel];
    
    
    // BUY --------------
    NSString *value = [Utility standardizeRateValue:[dict objectForKey:RATE_BUY_KEY]];
    float fValue = [Utility convertStringToFloat:value];
    [self setLable:value forLabel:_buyLabel];
    
    NSString *preValue = [Utility standardizeRateValue:[dict objectForKey:PRE_RATE_BUY_KEY]];
    float fPreValue = [Utility convertStringToFloat:preValue];
#ifdef __DUMMY_DATA__
    fPreValue = fValue + .3f;
#endif
    
    UIColor *textColor = COLOR_GREEN_NORMAL;
    
    if (fValue < fPreValue)
    {
        textColor = [UIColor redColor];
    }
    
    [_changedBuyLabel setTextColor:textColor];
    [_changedBuyLabel setText:[Utility makeChangedString:fValue
                                                preValue:fPreValue
                                         ignoreZeroValue:YES
                                    changedPerInDiffLine:is2Lines]];

    // TRANSFER
    value = [Utility standardizeRateValue:[dict objectForKey:RATE_TRANSFER_KEY]];
    fValue = [Utility convertStringToFloat:value];
    [self setLable:value forLabel:_transferLabel];
    
    preValue = [Utility standardizeRateValue:[dict objectForKey:PRE_RATE_TRANSFER_KEY]];
    fPreValue = [Utility convertStringToFloat:preValue];
#ifdef __DUMMY_DATA__
    fPreValue = fValue - .3f;
#endif
    
    textColor = COLOR_GREEN_NORMAL;
    
    if (fValue < fPreValue)
    {
        textColor = [UIColor redColor];
    }
    
    [_changedTransferLabel setTextColor:textColor];
    [_changedTransferLabel setText:[Utility makeChangedString:fValue
                                                     preValue:fPreValue
                                              ignoreZeroValue:YES
                                         changedPerInDiffLine:is2Lines]];
    
    // SELL ------------
    value = [Utility standardizeRateValue:[dict objectForKey:RATE_SELL_KEY]];
    fValue = [Utility convertStringToFloat:value];
    [self setLable:value forLabel:_sellLabel];
    
    preValue = [Utility standardizeRateValue:[dict objectForKey:PRE_RATE_SELL_KEY]];
    fPreValue = [Utility convertStringToFloat:preValue];
#ifdef __DUMMY_DATA__
    fPreValue = fValue - .3f;
#endif
    
    textColor = COLOR_GREEN_NORMAL;
    
    if (fValue < fPreValue)
    {
        textColor = [UIColor redColor];
    }
    
    [_changedSellLabel setTextColor:textColor];
    [_changedSellLabel setText:[Utility makeChangedString:fValue
                                                 preValue:fPreValue
                                          ignoreZeroValue:YES
                                     changedPerInDiffLine:is2Lines]];
}

@end
